package com.opennetexam.manager.utils;

import org.apache.log4j.Logger;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author naicha
 * Created on 2017/11/7.
 */
public class InternetHelper {

    private static final Logger logger = Logger.getLogger(InternetHelper.class);

    public static Boolean IsHostAlive(String ipAddress){
        //TODO :由于服务器禁用了“ping主机”方法，暂时禁用该判断功能，同时界面的“测试连接”按钮隐藏掉
        logger.info(ipAddress);
        return true;
//        if(ipAddress == null || ("").equals(ipAddress)){
//            return false;
//        }
//        //默认五秒
//        Integer timeOut = 5000;
//        boolean status = false;
//        try {
//            status = InetAddress.getByName(ipAddress).isReachable(timeOut);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return status;
    }

    /**
     * 判断输入的IP地址是否为ipv4
     * @param ip
     * @return
     * @throws UnknownHostException
     */
    public static Boolean isIPAddress(String ip){
        InetAddress byName = null;
        boolean flag = false;
        try {
            byName = InetAddress.getByName(ip);
            flag = byName instanceof Inet4Address;
        } catch (UnknownHostException e) {
            logger.error("ipFormatException");
        }
        return flag;
    }
}
